
<?php
if ($this->code == "0"){
    //echo "<legend>";
    echo $this->msg;
    //echo "</legend>";
}else echo $this->msg;

if ($this->code == ""){
?>


<legend>Edit Data Member</legend>
<div class="row clearfix">
    <div class="col-md-6 column">
            <input class="form-control" type="text" name="sncari" id="sncari" value="" placeholder="SN/Nama"></input>
            <button type="button" class="btn btn-default" onclick="getData()">Cari</button>
    </div>
</div>

<form method="post" id="frmEdit" name="frmEdit">
<br><br>
<div class="row clearfix">
    <div class="col-md-6 column">
        

        <div class="form-group">
                 <label for="sponsor_code">INTRODUCER</label>
                 <input id="sponsor_code" name="sponsor_code" type="text" placeholder="Sponsor"
                class="form-control" readonly value="<?php echo (isset($this->data["sponsor_name"])) ? $this->data["sponsor_name"].' ('.$this->data["sponsor_code"].')' : ""; ?>">
        </div>

    </div>
    <div class="col-md-6 column">

    </div>
</div>

<div class="row clearfix">
    <div class="col-md-6 column">

        <div class="form-group">
                <label for="sponsor_code">Tipe Member</label>
                <input id="member_type_name" name="member_type_name" type="text" placeholder="Sponsor"
                class="form-control" readonly value="<?php echo (isset($this->data["member_type_name"])) ? $this->data["member_type_name"] : ""; ?>">
                
        </div>
        <div class="form-group">
                <label for="member_name">Nama Member</label>
                 <input id="member_name" name="member_name" type="text" placeholder="Nama Member" 
                 class="form-control" value="<?php echo (isset($this->data["member_name"])) ? $this->data["member_name"] : ""; ?>">
        </div>
    </div>
    <div class="col-md-6 column">

        <div class="form-group">
                <label for="member_code">Kode Member</label>
                 <input id="member_code" name="member_code" type="text" placeholder="Kode Member"
                 class="form-control" readonly value="<?php echo (isset($this->data["member_code"])) ? $this->data["member_code"] : ""; ?>">
        </div>

        <div class="form-group">
                <label for="pin">PIN</label>
                <input id="pin" name="pin" type="password" placeholder="PIN" class="form-control" readonly value="<?php echo (isset($this->data["pin"])) ? $this->data["pin"] : ""; ?>">
            
        </div>


    </div>
</div>

<!--ALAMAT MEMBER -->
<br>
<div class="row clearfix">
    <div class="col-md-6 column">
        <h4>Alamat Member</h4>

        <!-- address-line1 input-->
        <div class="form-group">
            <label for="address_line1">Alamat 1</label>
            <input id="address_line1" name="address_line1" type="text" placeholder="Perumahan, Jalan, Blok, Nomor, RT/RW"
            class="form-control"  value="<?php echo (isset($this->data["addr1"])) ? $this->data["addr1"] : ""; ?>">
            <p class="help-block"></p>

        </div>
        <!-- address-line2 input-->
        <div class="form-group">
                <label for="address_line2">Alamat 2</label>
                <input id="address_line2" name="address_line2" type="text" placeholder="Kelurahan, Kecamatan"
                class="form-control"  value="<?php echo (isset($this->data["addr2"])) ? $this->data["addr2"] : ""; ?>">

        </div>
        <!-- city input-->
        <div class="form-group">
                <label for="city">Kota</label>
                <input id="city" name="city" type="text" placeholder="Kota" class="form-control"  value="<?php echo (isset($this->data["addr3"])) ? $this->data["addr3"] : ""; ?>">

        </div>
    </div>

    <div class="col-md-6 column">
        <h4>&nbsp;</h4>
        <!-- region input-->
        <div class="form-group">
                <label for="region">Provinsi</label>
                <input id="region" name="region" type="text" placeholder="Provinsi"
                class="form-control"  value="<?php echo (isset($this->data["addr4"])) ? $this->data["addr4"] : ""; ?>">

        </div>
        <!-- postal-code input-->
        <div class="form-group">
                <label for="postal_code">Kode Pos</label>
                <input id="postal_code" name="postal_code" type="text" placeholder="Kode Pos"
                class="form-control"  value="<?php echo (isset($this->data["addr5"])) ? $this->data["addr5"] : ""; ?>">

        </div>
        <!-- country select -->
        <div class="form-group">
                <label for="country">Negara</label>
                <select id="country" name="country" class="form-control" required>
                    <option value="">Pilih Negara</option>
                    <?php
                   foreach ($this->countryList as $oCountry) {                       
                        echo "<option value='".$oCountry['code']."'>". $oCountry['name'] ."</option>";
                   }
                   ?>
                </select>
                
                
        </div>
    </div>
</div>

<!--KONTAK MEMBER -->
<br>
<div class="row clearfix">
    <div class="col-md-6 column">
        <h4>Kontak Member</h4>

        <!-- contact_name input-->
        <div class="form-group">
                <label for="contact_name">Nama Kontak</label>
                <input id="contact_name" name="contact_name" type="text" placeholder="Nama Kontak"
                class="form-control"  value="<?php echo (isset($this->data["contact_name"])) ? $this->data["contact_name"] : ""; ?>">

        </div>
        <!-- mobile_no input-->
        <div class="form-group">
                <label for="mobile_no">HP Kontak</label>
                <input id="mobile_no" name="mobile_no" type="text" placeholder="No HP"
                class="form-control"  value="<?php echo (isset($this->data["mobile_no"])) ? $this->data["mobile_no"] : ""; ?>">

        </div>
        <!-- phone input-->
        <div class="form-group">
                <label for="phone">Tlp Kontak</label>
                <input id="phone" name="phone" type="text" placeholder="No Tlp"
                class="form-control"  value="<?php echo (isset($this->data["phone"])) ? $this->data["phone"] : ""; ?>">

        </div>
        <!-- email input-->
        <div class="form-group">
                <label for="email">Email Kontak</label>
                <input id="email" name="email" type="text" placeholder="Email" 
                class="form-control" value="<?php echo (isset($this->data["email"])) ? $this->data["email"] : ""; ?>">

        </div>
        <!-- no_ktp input-->
        <div class="form-group">
                <label for="no_ktp">No KTP</label>
                <input id="no_ktp" name="no_ktp" type="text" placeholder="No KTP" 
                class="form-control" value="<?php echo (isset($this->data["no_ktp"])) ? $this->data["no_ktp"] : ""; ?>">

        </div>

    </div>

    <div class="col-md-6 column">
        <h4>Ahli Waris</h4>

        <!-- contact_name input-->
        <div class="form-group">
                <label for="waris_name">Nama Ahli Waris</label>
                <input id="waris_name" name="waris_name" type="text" placeholder="Nama Ahli Waris"
                class="form-control"  value="<?php echo (isset($this->data["waris_name"])) ? $this->data["waris_name"] : ""; ?>">

        </div>
        <!-- mobile_no input-->
        <div class="form-group">
                <label for="waris_mobile_no">HP Ahli Waris</label>
                <input id="waris_mobile_no" name="waris_mobile_no" type="text" placeholder="No HP Ahli Waris"
                class="form-control"  value="<?php echo (isset($this->data["waris_mobile"])) ? $this->data["waris_mobile"] : ""; ?>">

        </div>
        <!-- phone input-->
        <div class="form-group">
                <label for="waris_phone">Tlp Ahli Waris</label>
                <input id="waris_phone" name="waris_phone" type="text" placeholder="No Tlp Ahli Waris"
                class="form-control"  value="<?php echo (isset($this->data["waris_phone"])) ? $this->data["waris_phone"] : ""; ?>">

        </div>
        <!-- email input-->
        <div class="form-group">
                <label for="waris_email">Email Ahli Waris</label>
                <input id="waris_email" name="waris_email" type="text" placeholder="Email Ahli Waris"
                class="form-control"  value="<?php echo (isset($this->data["waris_email"])) ? $this->data["waris_email"] : ""; ?>">

        </div>
        <!-- no_ktp input-->
        <div class="form-group">
                <label for="waris_ktp">No KTP</label>
                <input id="waris_ktp" name="waris_ktp" type="text" placeholder="No KTP Ahli Waris" 
                class="form-control" value="<?php echo (isset($this->data["waris_ktp"])) ? $this->data["waris_ktp"] : ""; ?>">

        </div>

    </div>
</div>

<!--AKUN BANK -->
<br>
<div class="row clearfix">
    <div class="col-md-6 column">
        <h4>Akun Bank</h4>

        <!-- bank_name input-->
        <div class="form-group">
                <label for="bank_name">Bank</label>
                <select name="bank_name" id="bank_name" class="form-control">
                    <option value="">Pilih Nama Bank</option>
                    <option value="BCA" <?php echo (isset($this->data["bank_name"]) && $this->data["bank_name"] == "BCA") ? "selected" : ""; ?>>BCA</option>
                    <option value="Mandiri" <?php echo (isset($this->data["bank_name"]) && $this->data["bank_name"] == "Mandiri") ? "selected" : ""; ?>>Mandiri</option>
                    <option value="BRI" <?php echo (isset($this->data["bank_name"]) && $this->data["bank_name"] == "BRI") ? "selected" : ""; ?>>BRI</option>
                    <option value="BNI" <?php echo (isset($this->data["bank_name"]) && $this->data["bank_name"] == "BNI") ? "selected" : ""; ?>>BNI</option>
                </select>

                

        </div>
        <!-- account_name input-->
        <div class="form-group">
                <label for="account_name">Nama Akun</label>
                <input id="account_name" name="account_name" type="text" placeholder="Nama Akun"
                class="form-control"  value="<?php echo (isset($this->data["account_name"])) ? $this->data["account_name"] : ""; ?>">

        </div>
    </div>

    <div class="col-md-6 column">
        <h4>&nbsp;</h4>

        <!-- bank_account_no input-->
        <div class="form-group">
                <label for="bank_account_no">Nomor Rekening</label>
                <input id="bank_account_no" name="bank_account_no" type="text" placeholder="Nomor Rekening"
                class="form-control"  value="<?php echo (isset($this->data["bank_account_no"])) ? $this->data["bank_account_no"] : ""; ?>">

        </div>
        <!-- bank_city input-->
        <div class="form-group">
                <label for="bank_city">Kota Bank</label>
                <input id="bank_city" name="bank_city" type="text" placeholder="Kota Bank"
                class="form-control"  value="<?php echo (isset($this->data["bank_city"])) ? $this->data["bank_city"] : ""; ?>">

        </div>
    </div>
    <div class="col-md-6 column">
        <button type="submit" class="btn btn-default" id="btnSubmit" name="btnSubmit">Simpan</button>
        <input type="hidden" name="member_id" id="member_id">
        <input type="hidden" name="member_contact_id" id="member_contact_id">
        <input type="hidden" name="contact_address_id" id="contact_address_id">
        <input type="hidden" name="member_contact_idw" id="member_contact_idw">
    </div>
</div>
</form>
<br>

<script>

    /* Attach a submit handler to the form */
    $("#frmEdit").submit(function(event) {

        /* Stop form from submitting normally */
        event.preventDefault();

        /* Clear result div*/
        $("#div_result").html('');
        $("#btnSubmit").html('Memproses...');
        $("#btnSubmit").attr('disabled','disabled');

        /* Get some values from elements on the page: */
        var values = $(this).serialize();

        var msg = [];

        /* Send the data using post and put the results in a div */

        $.ajax({
            url: "edit/",
            type: "post",
            data: values,
            success: function(response, opts){
                var cekSess = response.toUpperCase().search("SESSION TIMEOUT");

                if (cekSess < 0){
                    var cekStatus = response.toUpperCase().search("ERROR");

                    if (cekStatus < 0){

                        msg.push('<span>'+response+'</span>');
                        $.messager.alert('Info', msg.join('<br/>'));

                        //$("#div_result").html(resp);
                        $("#btnSubmit").removeAttr('disabled');
                        $("#btnSubmit").html('Submit');
                        
                    }else{

                        var resp = response.replace('ERROR', '');

                        msg.push('<span>'+resp+'</span>');
                        $.messager.alert('Info', msg.join('<br/>'));

                        //$("#div_result").html(resp);
                        $("#btnSubmit").removeAttr('disabled');
                        $("#btnSubmit").html('Submit');
                    }
                }else{
                    location.href='<?php echo $this->baseUrl() ?>/index/admin/msg/Session Timeout silahkan login kembali !';
                }
            },
            error:function(response, opts){
                //alert("failure");
                //$("#div_result").html(response);
                msg.push('<span>'+response+'</span>');
                $.messager.alert('Info', msg.join('<br/>'));

                $("#btnSubmit").removeAttr('disabled');
            }
        });

    });

    function getData(){

        var sn = $('#sncari').val();

        var msg = [];

        if (sn == "") msg.push('<span>* SN/Nama harus diisi</span>');

        if (msg.length > 0){
            $.messager.alert('Info', msg.join('<br/>'), 'info');
        }
        else{

            $.messager.progress();

            $.ajax({
            type: 'GET',
            dataType: 'json',
            url: '<?php echo $this->baseUrl() ?>/jsondata/memberdetail',
            data: {sn: sn},
            success: function(result){

                    $.messager.progress('close');

                    if (result.cnt > 0){
                        $('#member_id').val(result.member_id);
                        $('#member_code').val(result.member_code);
                        $('#member_type_name').val(result.member_type_name);
                        $('#member_name').val(result.member_name);
                        $('#sponsor_code').val(result.sponsor_code);
                        $('#pin').val(result.pin);

                        $('#address_line1').val(result.addr1);
                        $('#address_line2').val(result.addr2);
                        $('#city').val(result.addr3);
                        $('#region').val(result.addr4);
                        $('#postal_code').val(result.addr5);
                        $('#country').val(result.country);

                        $('#contact_name').val(result.contact_name);
                        $('#mobile_no').val(result.mobile_no);
                        $('#phone').val(result.phone);
                        $('#email').val(result.email);
                        $('#no_ktp').val(result.no_ktp);


                        $('#waris_name').val(result.waris_name);
                        $('#waris_mobile_no').val(result.waris_mobile);
                        $('#waris_phone').val(result.waris_phone);
                        $('#waris_email').val(result.waris_email);
                        $('#waris_ktp').val(result.waris_ktp);


                        $('#bank_name').val(result.bank_name);
                        $('#bank_account_no').val(result.bank_account_no);
                        $('#account_name').val(result.member_code);
                        $('#bank_city').val(result.bank_city);


                        $('#member_contact_id').val(result.member_contact_id);
                        $('#contact_address_id').val(result.contact_address_id);
                        $('#member_contact_idw').val(result.member_contact_idw);



                    }else{
                        $.messager.alert('Info', 'SN/Nama "'+sn+'" Tidak terdaftar!', 'info');
                        document.frmEditMember.reset();
                        $('#sncari').val(sn);
                    }
                    

                 }
            });

        }
    }

</script>

<?php } ?>